package edu.udlap.spring2008.is300.finalexam.app.mvc.view;

import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.ModelState;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.objects.ImageRecord;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.view.ViewComponent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

/**
 *
 * @author Ruslán Ledesma Garza
 */
public class ViewImageGridControls extends ViewComponent implements 
        MouseListener
{

    protected JScrollPane mainPane_;
    protected ScrollableFlowPanel contentPanel_ = new ScrollableFlowPanel();
    protected ArrayList<ImageRecord> imageSet;
    protected JLabel imageLabels_[];
    protected JLabel selectedImageLabel;

    public ViewImageGridControls()
    {
        
        imageLabels_ = new JLabel[0];
        
        contentPanel_.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        //contentPanel_.setMaximumSize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE));
        mainPane_ = new JScrollPane();
        mainPane_.setPreferredSize(new Dimension(400, 400));
        mainPane_.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        mainPane_.setAutoscrolls(true);
        mainPane_.setViewportView(contentPanel_);
    }
    
    protected void listenToControls()
    {
        for(int i=0; i<imageLabels_.length; i++)
            imageLabels_[i].addMouseListener(this);
    }
    
    protected void stopListeningControls()
    {
        for(int i=0; i<imageLabels_.length; i++)
            imageLabels_[i].removeMouseListener(this);
    }
    
    @Override
    public void setInitialState(ModelState modelState)
    {
        imageSetChanged(modelState);
    }

    @Override
    public void imageSetChanged(ModelState modelState)
    {
        stopListeningControls();
        
        ///remove all image labels and set contentPanel_ layout...
        {
            for(int i = 0; i<imageLabels_.length; i++)
                contentPanel_.remove(imageLabels_[i]);
            
            imageLabels_ = new JLabel[0];
        }
        
        ///get new set of images...
        imageSet =
            modelState.getMatchingImageRecords();
        
        ImageRecord selectedImageRecord =
                modelState.getSelectedImageRecord();
        
        ///if there are images, display them...
        if(imageSet.size() > 0)
        {
            ///create array of labels...
            imageLabels_ = new JLabel[imageSet.size()];

            ///create a dimension...
            Dimension d = new Dimension(150, 150);
            
            ///create each label...
            Iterator<ImageRecord> i = imageSet.iterator();
            ImageRecord imageRecord;
            int j = 0;
            while(i.hasNext())
            {
                imageRecord = i.next();
                imageLabels_[j] = new JLabel(
                            "<html>" +
                            "   <img src=\"file:" + imageRecord.getPath() + "\" " +
                            "   width=\"150\" " +
                            "   height=\"150\" >" +
                            "</html>");
                
                imageLabels_[j].setPreferredSize(d);
                imageLabels_[j].setMinimumSize(d);
                imageLabels_[j].setMaximumSize(d);
                
                if(imageRecord.equals(selectedImageRecord))
                    selectionChanged(modelState);
                
                contentPanel_.add(imageLabels_[j++]);
            }
        }
        
        contentPanel_.validate();
        contentPanel_.repaint();
        mainPane_.validate();
        mainPane_.repaint();
        
        listenToControls();
    }

    @Override
    public void selectionChanged(ModelState modelState)
    {
        if(selectedImageLabel != null)
            selectedImageLabel.setBorder(null);
        
        ImageRecord imageRecord =
                modelState.getSelectedImageRecord();
        
        if(imageRecord == null)
            return;
        
        selectedImageLabel = imageLabels_[imageSet.indexOf(imageRecord)];
        
        selectedImageLabel.setBorder(new LineBorder(Color.BLUE, 5));
    }

    @Override
    public void favoritesChanged(ModelState modelState)
    {
        ///do nothing...
    }

    @Override
    public void slideShowMode()
    {
        ///do nothing...
    }

    @Override
    public void gridMode()
    {
        ///do nothing...
    }

    @Override
    public Component getComponent()
    {
        return (Component)mainPane_;
        //return (Component)contentPanel_;
    }

    public void mouseClicked(MouseEvent e)
    {
        ///do nothing...
    }

    public void mousePressed(MouseEvent e)
    {
        ///do nothing...
    }

    public void mouseReleased(MouseEvent e)
    {
        if(controller_ == null)
            return;
        
        Object source = e.getSource();
        
        for(int i=0; i<imageLabels_.length; i++)
        {
            if(source == imageLabels_[i])
            {
                controller_.setSelectedImageRecord(imageSet.get(i).getId());
            }       
        }
    }

    public void mouseEntered(MouseEvent e)
    {
        ///do nothing...
    }

    public void mouseExited(MouseEvent e)
    {
        ///do nothing...
    }

}
